﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms;
using System.Drawing;

namespace Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control
{
    /// <summary>
    /// A container button class associated with a certain message
    /// </summary>
    public class AEUCSerializableButton : Control, IAEUCSerialization
    {
        int associate_message_reference;
        string button_name;

        #region Constructors
        /// <summary>
        /// Constructor creating the serializable button container with no associated message
        /// </summary>
        public AEUCSerializableButton()
        {
            associate_message_reference = -1;
            button_name = "Serializable button";
            Location = new Point(0,0);
        }
        /// <summary>
        /// Constructor creating the button container with no associated message
        /// </summary>
        /// <param name="iamr">Associated message reference</param>
        /// <param name="bnText">Button control text</param>
        /// <param name="iLoc">Button control location</param>
        public AEUCSerializableButton(int iamr, string bnText, Point iLoc)
        {
            associate_message_reference = iamr;
            button_name = bnText;
            Location = iLoc;
        }
        #endregion
        /// <summary>
        /// Reads the deserialized button information into the provided fields
        /// </summary>
        /// <param name="iamr">Associated message reference</param>
        /// <param name="bnText">Button control text</param>
        /// <param name="iLoc">Button control location</param>
        public void GetButtonInformation(out int iamr, out string bnText, out Point iLoc)
        {
            iamr = associate_message_reference;
            bnText = button_name;
            iLoc = Location;
        }

        #region IAEUCSerialization Members
        /// <summary>
        /// Current element serialization state version
        /// </summary>
        public int serVersion
        {
            get
            {
                return 0x000;
            }
        }
        /// <summary>
        /// Saves this objec instance to a file
        /// </summary>
        /// <param name="srWriter">Binray writer of the associated file</param>
        public void Serialize(System.IO.BinaryWriter srWriter)
        {
            srWriter.Write(serVersion);
            Serialize_v000(srWriter);
        }
        /// <summary>
        /// Loads this objects instance from a file
        /// </summary>
        /// <param name="srReader">Binray reader of the associated file</param>
        /// <returns>Boolean representing the success of the operation</returns>
        public bool Deserialize(System.IO.BinaryReader srReader)
        {
            bool desProcState;
            int serialVer = srReader.ReadInt32();

            switch (serialVer)
            {
                case 0x000:
                    desProcState = Deserialize_v000(srReader);
                    break;
                default:
                    desProcState = false;
                    break;
            }

            return desProcState;
        }

        #region Version 0x000 serialization
        /// <summary>
        /// Version 0x000 serialization
        /// </summary>
        /// <param name="srWriter">File writer</param>
        void Serialize_v000(System.IO.BinaryWriter srWriter)
        {
            srWriter.Write(associate_message_reference);
            srWriter.Write(button_name);

            srWriter.Write(Location.X);
            srWriter.Write(Location.Y);
        }
        /// <summary>
        /// Version 0x000 deserialization
        /// </summary>
        /// <param name="srReader">File reader</param>
        /// <returns>True if operation is succesful</returns>
        bool Deserialize_v000(System.IO.BinaryReader srReader)
        {
            bool NoError = true;
            try
            {
                associate_message_reference = srReader.ReadInt32();
                button_name = srReader.ReadString();
                Location = new Point(srReader.ReadInt32(), srReader.ReadInt32());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                NoError = false;
            }
            return NoError;
        }
        #endregion
        #endregion
    }
}